package com.we823.project.controller.report;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.project.common.file.MaterialStockOutUtil;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.StationModel;
import com.we823.project.model.UserModel;
import com.we823.project.model.UserStationModel;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.model.repo.MaterialStockOutModel;
import com.we823.project.repository.service.StationService;
import com.we823.project.repository.service.UserService;
import com.we823.project.repository.service.UserStationService;
import com.we823.project.repository.service.repo.MaterialService;
import com.we823.project.repository.service.repo.MaterialStockOutService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.REPORT_STOCK_OUT)
public class StockOutReportController extends AbstractBaseReportController<MaterialStockOutModel> {

	private String baseUrl = WebConstants.REPORT_STOCK_OUT_TEMPLATE;
	
	@Autowired
	private StationService stationService;
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserStationService userStationService;
	@Autowired
	private MaterialService materialService;
	
	private MaterialStockOutService materialStockOutService;
	
	@Autowired
	public StockOutReportController(MaterialStockOutService materialStockOutService) {
		super(materialStockOutService);
		this.materialStockOutService = materialStockOutService;
		super.baseUrl = baseUrl;
		
		setResourceIdentity("report:stock_out");
	}

	@Override
	public void expendInfo(Model model) {
		List<StationModel> stationModels = stationService.findAll();
		model.addAttribute("stations",stationModels);
	}

	@Override
	public boolean export(String fullname, String sheetname,
			List<MaterialStockOutModel> models) {
		MaterialStockOutUtil materialStockOutUtil = new MaterialStockOutUtil();
		return materialStockOutUtil.createExcel2007(fullname, sheetname, models);
	}

	@Override
	public Page<MaterialStockOutModel> getData(PageRequest pageRequest,
			HttpServletRequest request, Model model, boolean pageable) {
		Page<MaterialStockOutModel> materialStockOutPage = null;
		
		int stationId = RequestUtil.getValueByParam(request, "stationId", 0);
		int year = RequestUtil.getValueByParam(request, "year", 0);
		int month = RequestUtil.getValueByParam(request, "month", 0);
		int day = RequestUtil.getValueByParam(request, "day", 0);
		if(!pageable) {
			pageRequest= null;
		}
		
		if(stationId==0) {
			if(year==0) {
				materialStockOutPage = materialStockOutService.findAll(pageRequest);
			}else {
				materialStockOutPage = materialStockOutService.findByEdittime(year,month,day,pageRequest);
			}
		}else {
			List<UserStationModel> userStationModels = userStationService.findByStationId(stationId);
			if(userStationModels!=null) {
				Set<Integer> userIds = new HashSet<>();
				for (UserStationModel userStationModel : userStationModels) {
					userIds.add(userStationModel.getUserId());
				}
				if(userIds.size()>0) {
					materialStockOutPage = materialStockOutService.findByEdittimeAndUserid(year,month,day,userIds,pageRequest);
				}
			}
		}
		fillInfo(materialStockOutPage);
		return materialStockOutPage;
	}

	private void fillInfo(Page<MaterialStockOutModel> materialStockOutPage) {
		if(materialStockOutPage!=null) {
			List<MaterialStockOutModel> materialStockOutModels = materialStockOutPage.getContent();
			if(materialStockOutModels!=null) {
				Map<Integer, UserStationModel> userStationMap = userStationService.map();
				Map<Integer, UserModel> userMap = userService.map();
				Map<Integer, StationModel> stationMap = stationService.map();
				Map<String, MaterialModel> materialMap = materialService.getMapByCategory(null);
				for (MaterialStockOutModel materialStockOutModel : materialStockOutModels) {
					if(materialMap.containsKey(materialStockOutModel.getMaterialCode())) {
						MaterialModel materialModel = materialMap.get(materialStockOutModel.getMaterialCode());
						if(materialModel!=null) {
							materialStockOutModel.setMaterialName(materialModel.getName());
							materialStockOutModel.setUnit(materialModel.getUnit());
							String color = materialModel.getColor();
							if(StringUtils.isEmpty(color)) {
								materialStockOutModel.setModel(materialModel.getFullname());
							}else {
								materialStockOutModel.setModel(materialModel.getFullname()+"("+color+")");
							}
						}else {
							materialStockOutModel.setMaterialName("未知");
						}
					}
					int userId = materialStockOutModel.getUserId();
					if(userStationMap.containsKey(userId)) {
						int stationId = userStationMap.get(userId).getStationId();
						if(stationMap.containsKey(stationId)) {
							StationModel stationModel = stationMap.get(stationId);
							if(stationModel!=null) {
								materialStockOutModel.setStationName(stationModel.getName());
							}else {
								materialStockOutModel.setStationName("未知");
							}
						}
					}
					
					if(userMap.containsKey(userId)) {
						UserModel userModel = userMap.get(userId);
						if(userModel!=null) {
							String name = userModel.getRealname();
							if (StringUtils.isEmpty(name)) {
								name = userModel.getUsername();
							}
							materialStockOutModel.setUserName(name);
						}else {
							materialStockOutModel.setUserName("未知");
						}
					}
				}
			}
		}
		
	}

	public StationService getStationService() {
		return stationService;
	}

	public void setStationService(StationService stationService) {
		this.stationService = stationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserStationService getUserStationService() {
		return userStationService;
	}

	public void setUserStationService(UserStationService userStationService) {
		this.userStationService = userStationService;
	}

	public MaterialStockOutService getMaterialStockOutService() {
		return materialStockOutService;
	}

	public void setMaterialStockOutService(
			MaterialStockOutService materialStockOutService) {
		this.materialStockOutService = materialStockOutService;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	
}
